﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Data;
using System.ComponentModel;
using DotSpatial.Topology;
using DotSpatial.Symbology;
using DotSpatial.Controls;
using System.Data;
using System.Diagnostics;

namespace HAGISV2
{

    /// <summary>
    /// 为dotspatial 提供单一dataset 图层基础模型
    /// </summary>
    [Serializable]
   public class SingleBaseLayer:Layer
    {
        public SingleBaseLayer(string id)
            : base(id)
        {
            this._isSelectable = true;
        }





        #region 属性

        /// <summary>
        /// 用于保存当前图形的类型,type将根据子类型来确定
        /// </summary>
        protected FeatureType _featureType;
     



        [NonSerialized]
        protected IMapLayer _iLayer;


        [Browsable(false)]     //本属性不可见
        public virtual IMapLayer ILayer
        {
            get { return _iLayer; }
            set { _iLayer = value; }
        }

        [NonSerialized]
        protected DotSpatial.Data.DataSet _dataSet;

        [Browsable(false)]
        public DotSpatial.Data.DataSet dataSet
        {
            get { return _dataSet; }
            set { _dataSet = value; }
        }

        #endregion
     



        #region 公用配置信息


        ///// <summary>
        ///// 用于初始化shape相关的选项
        ///// </summary>
        public virtual void BuildDataset()
        {


            dataSet = new FeatureSet(this._featureType);

            this.dataSet.Projection = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

            DataColumn dcId = new DataColumn("id", typeof(string));

            DataColumn dcDesc = new DataColumn("desc", typeof(string));



            (this.dataSet as IFeatureSet).DataTable.Columns.Add(dcId);
            (this.dataSet as IFeatureSet).DataTable.Columns.Add(dcDesc);

            (this.dataSet as IFeatureSet).DataTable.TableName = this.Id;



        }

        #endregion


       


        #region 需要重写的方法

        public override Extent GetLayerExtent()
        {
            if (!this.IsLoaded)
            {
                return null;
            }

            switch (ViewMode)
            {
                case FMAPVIEW.NORMAL:
                    if (this.dataSet as FeatureSet != null)
                        if ((this.dataSet as FeatureSet).Features.Count <= 0)
                            return null;

                    return this._iLayer.Extent;

                case FMAPVIEW.FRONT:
                    break;
                case FMAPVIEW.RIGHT:
                    break;
                default:
                    break;
            }

            return null;
        }

        public override void Redraw()
        {
            base.Redraw();
        }

        public override void Render()
        {
            Map map = this.DefaultManager.CurrentMap;

            this._iLayer = map.Layers.Add(this.dataSet);

            if (this._mapLayerIndex != -1)
            {
                this._iLayer.LockDispose();

                map.Layers.Remove(this._iLayer);

                map.Layers.Insert(this._mapLayerIndex, this._iLayer);

                this._iLayer.UnlockDispose();

                this._mapLayerIndex = -1;
            }

            this._isLoaded = true;

            this.OnSetVisible();
        }

        public override void StopRender()
        {

            Map map = this.DefaultManager.CurrentMap;

            map.Layers.Remove(this.ILayer);

            this._isLoaded = false;

          
        }

        protected override void OnSetVisible()
        {
            if (!this.IsLoaded)
            {
                return;
            }

            this._iLayer.IsVisible = this.Visible;
        }

        public override bool SaveLayer()
        {
            return base.SaveLayer();
        }

        protected void LoadFeatures()
        {

          









        }

        protected void SaveFeatures()
        {
            //this._dataSet.save

            //this._ptFeatureSet.SaveAs(this.LayerFileNamePt, true);
            //this._lnFeatureSet.SaveAs(this.LayerFileNameLn, true);
            //this._pyFeatureSet.SaveAs(this.LayerFileNamePy, true);

        }

        public override bool OpenLayer()
        {

            this.BuildDataset();
            this.Render();
           

            return base.OpenLayer();
        }

        public override void Select(IEnvelope tolerant, IEnvelope strict, SelectionMode selectionMode)
        {
            base.Select(tolerant, strict, selectionMode);
        }


        #endregion




        //protected override void BuildLayer()
        //{

        //    base.BuildLayer();

        //    this.dataSet.Projection = this.m_ParenLManager.CurrentMap.Projection;

        //    this.ILayer = this.m_ParenLManager.CurrentMap.Layers.Add(this.dataSet);

        //    (this.dataSet as IFeatureSet).Filename = this.AbsFileName;



        //}


        //public override void SaveEntity()
        //{
        //    try
        //    {
        //        (this.dataSet as FeatureSet).SaveAs(this.AbsFileName, true);
        //    }
        //    catch (Exception ex)
        //    {
        //    }

        //}


        //public override void SaveProperty(string filepath)
        //{
        //    base.SaveProperty(filepath);

        //    if (!System.IO.Directory.Exists(filepath))
        //    {
        //        System.IO.Directory.CreateDirectory(filepath);
        //    }
        //}

        //public override void LoadEntity()
        //{
        //    try
        //    {
        //        //this.DataSet = (FeatureSet)FeatureSet.OpenFile(this.AbsFileName);
        //        (this.dataSet as FeatureSet).Open(this.AbsFileName); //刘改，把上句改成了这句

        //    }
        //    catch (Exception ex)
        //    {

        //        this.Configure();

        //        foreach (FShape shp in this.m_Shapes.Values)
        //        {
        //            Shape polyshp = new Shape(this._type);
        //            if (this._type == FeatureType.Point)
        //            {
        //                Coordinate pt = shp.normalPts[0];
        //                polyshp = new Shape(pt);
        //            }
        //            else
        //            {
        //                polyshp.AddPart(shp.normalPts.ToArray(), CoordinateType.Regular);
        //            }
        //            Feature f = new Feature(polyshp);

        //            if (f != null)
        //                (this.dataSet as IFeatureSet).Features.Add(f);
        //            f.DataRow["id"] = shp.Id;
        //            f.DataRow["desc"] = shp.descString;


        //        }
        //    }

        //}



        //public override void LoadProperty(string filepath)
        //{
        //    base.LoadProperty(filepath);
        //}


        ///// <summary>
        ///// 用于验证shape类是否有效
        ///// </summary>
        ///// <param name="shp"></param>
        ///// <returns></returns>
        //public virtual bool IsValidShape(FShape shp)
        //{
        //    if (shp.normalPts.Count < 1)
        //    {
        //        return false;

        //    }

        //    if (this._type == FeatureType.Line)
        //    {
        //        if (shp.normalPts.Count < 2)
        //        {
        //            return false;

        //        }

        //    }


        //    if (this._type == FeatureType.Polygon)
        //    {
        //        if (shp.normalPts.Count < 3)
        //        {
        //            return false;

        //        }

        //    }


        //    return true;


        //}

        //[NonSerialized]
        //private List<IFeature> _selectedFeatures;




        ///// <summary>
        ///// 自定义FShape对应的Feature的修改
        ///// </summary>
        ///// <param name="Shp"></param>
        //private void ChangeFeature(FShape NewShp)
        //{
        //    foreach (IFeature f in (this.dataSet as IFeatureSet).Features)
        //    {
        //        if (f.DataRow["id"].ToString() == NewShp.Id)
        //        {
        //            Shape polyshp = new Shape(this._type);
        //            if (this._type == FeatureType.Point)
        //            {
        //                Coordinate pt = NewShp.normalPts[0];
        //                polyshp = new Shape(pt);
        //            }
        //            else
        //            {
        //                polyshp.AddPart(NewShp.normalPts.ToArray(), CoordinateType.Regular);
        //            }
        //            Feature newf = new Feature(polyshp);
        //            //因为无法得知是移动了shp还是修改了shp的形状或坐标
        //            //所以对创建一个新的Feature,将新Feature的坐标直接赋给旧的Feature
        //            //这样还可以保证字段selectedFeatures——已选择的Feature 的同步修改（因为是引用传递）
        //            f.Coordinates = newf.Coordinates;
        //            f.DataRow["desc"] = NewShp.descString;

        //            (this.dataSet as IFeatureSet).InitializeVertices();
        //            (this.dataSet as IFeatureSet).UpdateExtent();
        //            break;
        //        }
        //    }

        //}









        ///// <summary>
        ///// 主要是用来单个图层要关闭一些参数
        ///// </summary>
        //public override void CloseLayer()
        //{
        //    base.CloseLayer();
        //}



        ////=========================================================================================//

        ///// <summary>
        ///// 复制属性
        ///// </summary>
        ///// <param name="layer"></param>
        //public override void CopyProperties(FLayer flayer)
        //{
        //    base.CopyProperties(flayer);

        //    FShapeLayer layer = flayer as FShapeLayer;
        //    if (layer == null) return;

        //    //this._LabelLayer = layer.LabelLayer;

        //}






        public FShape GetShapeByID(string id)
        {
            throw new NotImplementedException();
        }

        public void AddShape(FShape shp)
        {
            throw new NotImplementedException();
        }

        public void AddShapeRange(List<FShape> lstShp)
        {
            throw new NotImplementedException();
        }

        public void RemoveShape(string id)
        {
            throw new NotImplementedException();
        }

        public void RemoveShapeRange(List<string> lstIds)
        {
            throw new NotImplementedException();
        }

        public void ModifyShape(FShape NewShp)
        {
            throw new NotImplementedException();
        }

        public void ModifyShapeRange(List<FShape> lstShps)
        {
            throw new NotImplementedException();
        }

        public void UpdateShapes(List<FShape> lstShps)
        {
            throw new NotImplementedException();
        }

        public void ClearShapes()
        {
            throw new NotImplementedException();
        }
    }
}
